package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntitySysRole;
import com.loongtech.bi.entity.system.EntitySysRoleFunction;
import com.loongtech.bi.entity.system.EntitySysUserRole;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysRoleFunctionManager;
import com.loongtech.bi.manager.system.SysRoleManager;
import com.loongtech.bi.manager.system.SysUserProjectRoleManager;
import com.loongtech.bi.support.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色管理
 * 
 * @author xckuang
 * @date 2014-10-06
 */
@Controller("roleManagerAction")
@Scope("view")
public class RoleManagerAction implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private Session session;
	@Autowired
	private SysLogManager sysLogManager;	
	@Autowired
	private SysRoleManager sysRoleManager;
	@Autowired
	private SysUserProjectRoleManager sysUserProjectRoleManager;
	@Autowired
	private SysRoleFunctionManager sysRoleFunctionManager;
	private EntitySysRole role;
	private List<EntitySysRole> roleList = new ArrayList<EntitySysRole>();
	private static final String MsgTag ="角色信息管理";

	@Autowired
	public void init() {
		setRole(new EntitySysRole());
	}

	private boolean empty(String param) {
		return null == param || param.trim().isEmpty();
	}

	public void search() {
		List<Object> params = new ArrayList<Object>();
		if (role.getId() > 0) {
			params.add(EntitySysRole.K.id);
			params.add(role.getId());
		}
		if (role.getTypes() > 0) {
			params.add(EntitySysRole.K.types);
			params.add(role.getTypes());
		}
		if (!empty(role.getName())) {
			params.add(EntitySysRole.K.name);
			params.add(role.getName());
		}
		if (!empty(role.getDescription())) {
			params.add(EntitySysRole.K.description);
			params.add(role.getDescription());
		}
		roleList.clear();
		if (params.isEmpty()) {
			roleList.addAll(sysRoleManager.findAll());
		} else {
			roleList.addAll(sysRoleManager.findBy(params.toArray(new Object[params.size()])));
		}
		sysLogManager.addLog(session.getUserName(), "角色查询成功!");
	}

	public void modify(EntitySysRole role) {
		FacesContext context = FacesContext.getCurrentInstance();
		EntitySysRole cur = sysRoleManager.get(role.getId());
		if (cur != null) {
			sysRoleManager.merge(role);
			sysLogManager.addLog(session.getUserName(), "角色信息修改成功!roleId="+role.getId());
			context.addMessage(MsgTag, new FacesMessage("角色信息修改成功", "角色id="+role.getId()));
			search();
		}else{
			sysLogManager.addLog(session.getUserName(), "角色信息修改失败,不存在角色roleId=="+role.getId());
			context.addMessage(MsgTag, new FacesMessage("角色信息修改失败", "不存在角色roleId=="+role.getId()));
		}
	}

	public void delete(int roleId) {
		FacesContext context = FacesContext.getCurrentInstance();
		List<EntitySysUserRole> list1 = sysUserProjectRoleManager.findAll(EntitySysUserRole.K.roleId,roleId);
		if(null != list1 && !list1.isEmpty()){
			context.addMessage(MsgTag, new FacesMessage("角色删除失败", "角色已经绑定用户!"));
			return;
		}
		List<EntitySysRoleFunction> list2 = sysRoleFunctionManager.findAll(EntitySysRoleFunction.K.roleId,roleId);
		if(null != list2 && !list2.isEmpty()){
			context.addMessage(MsgTag, new FacesMessage("角色删除失败", "角色已经绑定功能!"));
			return;
		}		
		if(null != sysRoleManager.get(roleId)){
			sysRoleManager.removeById(roleId);
			sysLogManager.addLog(session.getUserName(), "角色删除成功!roleId="+roleId);
			context.addMessage(MsgTag, new FacesMessage("角色删除成功", "角色roleId="+roleId));
			search();
		}	
	}

	public EntitySysRole getRole() {
		return role;
	}

	public void setRole(EntitySysRole role) {
		this.role = role;
	}

	public List<EntitySysRole> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<EntitySysRole> roleList) {
		this.roleList = roleList;
	}	 
}